#include "Range.h" 

#include <QSlider>
#include <QLineEdit>
#include <QLabel>
#include <QVBoxLayout>

Range::Range(QWidget *parent) : QWidget(parent) 
{
	init();
}
Range::Range(const QString &text, QWidget *parent) : QWidget(parent) 
{
	init();
	setText(text);
}

void Range::init() 
{
	slider = new QSlider(Qt::Horizontal);
	slider->setRange(0, 1000);
	slider->setValue(0);

	number = new QLineEdit();
	number->setText(tr("0"));
	number->setAlignment(Qt::AlignHCenter);

	label = new QLabel();
	label->setAlignment(Qt::AlignHCenter);

	connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
	connect(number, SIGNAL(textChanged(const QString &)), this, SLOT(setValue(const QString &)));
	connect(slider, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));

	QVBoxLayout *layout = new QVBoxLayout;
	layout->addWidget(slider);
	layout->addWidget(number);
	layout->addWidget(label);

	setLayout(layout);
}

void Range::setText(const QString &text) 
{
	label->setText(text);
}

void Range::setValue(int value) 
{
	slider->setValue(value);
	number->setText(QString::number(value));
}

void Range::setValue(const QString &value) 
{
	slider->setValue(value.toInt());
	number->setText(value);
}

void Range::setRange(int lower, int higher) 
{
	if (lower < 0 || higher > 9999 || lower > higher) {
         qWarning("LCDRange::setRange(%d, %d)\n"
                  "\tRange must be 0..9999\n"
                  "\tand minValue must not be greater than maxValue",
                  lower, higher);
         return;
     }
     slider->setRange(lower, higher);

}

int Range::value()
{
	return slider->value();
}
